﻿// Original author contact info: Owen Emlen (owene_1998@yahoo.com)
// Note: other individuals may also have contributed to this code
// Project hosted on CodePlex.com as of 1/10/2009 at http://www.codeplex.com/EmlenMud
using System;
using System.Xml.Serialization;
using System.ComponentModel;
using System.Reflection;
using System.Threading;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BrainTechLLC.ThreadSafeObjects;
using System.Runtime.Serialization;

namespace BrainTechLLC
{
    /// <summary>
    /// Stores and keeps track of all currently-logged in players
    /// </summary>
    [Serializable]
    [DataContract]
    public class CurrentPlayerCollection : AssociatedIDCollection, IAssociatedIDCollection 
    {
        public static CurrentPlayerCollection GetCurrentPlayerCollectionSnapshot(IEventPerceiver perceiver)
        {
            // TODO - take into account the perceiver when compiling the list
            CurrentPlayerCollection results = new CurrentPlayerCollection();
            
            if (UsernamesPasswords.Instance == null)
                return results;

            List<UsernamePassword> allLogins = UsernamesPasswords.Instance.AllItems;
                    
            foreach (var login in allLogins)
            {
                long id = login.IdIfCurrentlyPlaying;

                if (id > 0)
                {
                    GameChar gameChar = Everything.Find<GameChar>(id);

                    if (gameChar != null && perceiver.CanPerceive(gameChar, ViewType.Sense))
                        results.Add(id);
                }
            }

            return results;
        }
    }
}
